package com.whxy02.tolearn.app.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.whxy02.tolearn.app.beans.*;
import com.whxy02.tolearn.app.resultObject.CollectionResultType;
import com.whxy02.tolearn.app.resultObject.ResultState;
import com.whxy02.tolearn.app.service.*;
import com.whxy02.tolearn.app.utils.PackResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/collections")
public class Collections {

    @Resource
    ExamCollectionService examCollectionService;
    @Resource
    QuestionCollectionService questionCollectionService;
    @Resource
    ExamService examService;
    @Resource
    QuestionService questionService;
    @Resource
    PackResult packResult;
    @Resource
    SubjectService subjectService;
    @Resource
    TagsService tagsService;

    @RequestMapping("/{target}Collection/add")
    public Map<String,Object> addCollection(HttpServletRequest request, @PathVariable("target")String target, @RequestParam(value = "questionId",required = false)Integer questionId){
        User user= (User) request.getAttribute("user");
        HttpSession session=request.getSession();
        Integer examId= (Integer) session.getAttribute("examId");
        try {
            switch (target) {
                case "exam":
                    ExamCollection collection = examCollectionService.getByUserAndExam(user.getUserId(), examId);
                    if(collection==null){
                        collection=new ExamCollection();
                        collection.setUserId(user.getUserId());
                        collection.setExamId(examId);
                        examCollectionService.save(collection);
                        Exam exam= examService.getById(collection.getExamId());
                        exam.setStars(exam.getStars()+1);
                        examService.change(exam);
                        return packResult.doPackToMap(ResultState.SUCCESS);
                    }else {
                        return packResult.doPackToMap(ResultState.EXIST_COLLECTION);
                    }
                case "question":
                    QuestionCollection collection1 = questionCollectionService.getByUserAndQuestion(user.getUserId(),questionId);
                    if(collection1==null){
                        collection1=new QuestionCollection();
                        collection1.setQuestionId(questionId);
                        collection1.setUserId(user.getUserId());
                        questionCollectionService.save(collection1);
                        Question question= questionService.getById(collection1.getQuestionId());
                        question.setStars(question.getStars()+1);
                        questionService.change(question);
                        return packResult.doPackToMap(ResultState.SUCCESS);
                    }else {
                        return packResult.doPackToMap(ResultState.EXIST_COLLECTION);
                    }

                default:
                    return packResult.doPackToMap(ResultState.NOTHING_TO_DO);
            }
        }catch (Exception e){
            e.printStackTrace();
            return packResult.doPackToMap(ResultState.SERVER_ERR);
        }

    }

    @RequestMapping("/search/{target}/{nowPage}")
    public Map<String,Object> getCollection(HttpServletRequest request,@PathVariable("target")String target,@PathVariable("nowPage")Integer nowPage){
        User user= (User) request.getAttribute("user");
        CollectionResultType collectionResultType=new CollectionResultType();
        try {
            switch (target) {
                case "exam":

                    Page<ExamCollection> pageE= examCollectionService.getByUserIdAndDateDescPage(user.getUserId(), nowPage);
                    List<Exam> exams=new ArrayList<>();
                    for(ExamCollection e:pageE.getRecords()){
                        if(e!=null){
                            Exam exam= examService.getById(e.getExamId());
                            Subject subject= subjectService.getById(exam.getSubjectId());
                            Tags tags= tagsService.getById(exam.getTagsId());
                            if(subject!=null){
                                exam.setSubject(subject.getSubjectName());
                            }
                            if(tags!=null){
                                exam.setTags(tags.getTagsName());
                            }
                            exams.add(exam.setCollectionTime(e.getCreateTime()));
                        }

                    }


                    collectionResultType.setExamCollections(exams);
                    collectionResultType.setCurrent((int) pageE.getCurrent());
                    collectionResultType.setPages((int) pageE.getPages());
                    collectionResultType.setSize((int) pageE.getSize());

                    return packResult.doPackToMap(ResultState.SUCCESS,collectionResultType);
                default:
                    Page<QuestionCollection> pageQ= questionCollectionService.getByUserIdAndDateDescPage(user.getUserId(), nowPage);
                    List<Question> questions=new ArrayList<>();
                    for(QuestionCollection q:pageQ.getRecords()){
                        if(q!=null){
                            Question question= questionService.getById(q.getQuestionId());
                            Subject subject= subjectService.getById(question.getSubjectId());
                            Tags tags= tagsService.getById(question.getTagsId());
                            if(subject!=null){
                                question.setSubject(subject.getSubjectName());
                            }
                            if(tags!=null){
                                question.setTags(tags.getTagsName());
                            }
                            questions.add(question.setCollectionTime(q.getCreateTime()));
                        }
                    }

                    collectionResultType.setQuestionCollections(questions);
                    collectionResultType.setCurrent((int) pageQ.getCurrent());
                    collectionResultType.setPages((int) pageQ.getPages());
                    collectionResultType.setSize((int) pageQ.getSize());

                    return packResult.doPackToMap(ResultState.SUCCESS,collectionResultType);
            }
        }
        catch (Exception e){
            e.printStackTrace();
            return packResult.doPackToMap(ResultState.SERVER_ERR,e);
        }

    }

}
